{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chat dialog\n",
    "\n",
    "Guidance supports chat-based models like ChatGPT and GPT-4 using role tags. These are then converted to the appropriate format for the model (either a JSON API format or special tokens)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import guidance\n",
    "import re\n",
    "guidance.llm = guidance.llms.OpenAI(\"gpt-4\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Multi-step chat with hidden blocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-8be60394-2c30-4349-b979-bcd391b5cc89\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-8be60394-2c30-4349-b979-bcd391b5cc89\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful assistant.</div></div><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25);'></span><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>I want to <span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{goal}}'>read more books</span>.<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25);'></span>\n",
       "Here is my plan:\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{parse_best prosandcons options}}'>Set a daily reading goal, such as one chapter a day.</span>\n",
       "Please elaborate on this plan, and tell me how to best accomplish it.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='{{gen &#x27;plan&#x27; max_tokens=500}}'>To best accomplish your daily reading goal and create a successful plan, follow these steps:\n",
       "\n",
       "1. Choose the right books: Select books that genuinely interest you and match your reading level. This will make it easier to stay engaged and motivated.\n",
       "\n",
       "2. Break down your goal: If your goal is to read one chapter a day, determine the average number of pages in a chapter and set a specific page count goal. This will help you track your progress more effectively.\n",
       "\n",
       "3. Create a reading schedule: Set aside a specific time each day for reading, such as before bed or during your lunch break. Consistency is key to forming a habit.\n",
       "\n",
       "4. Minimize distractions: Find a quiet, comfortable space to read where you can focus without interruptions. Turn off your phone or put it on silent mode to avoid distractions.\n",
       "\n",
       "5. Track your progress: Use a reading log or journal to record the books you&#x27;ve read, the number of pages or chapters completed, and any thoughts or insights you&#x27;ve gained. This will help you stay motivated and see your progress over time.\n",
       "\n",
       "6. Stay accountable: Share your reading goal with a friend or family member who can help keep you accountable. You can also join a book club or online reading community to discuss your progress and share recommendations.\n",
       "\n",
       "7. Reward yourself: Celebrate your achievements by treating yourself to something you enjoy, such as a favorite snack or an extra episode of your favorite TV show. This will help reinforce your reading habit.\n",
       "\n",
       "8. Be flexible: If you find that your daily reading goal is too ambitious or not challenging enough, adjust it accordingly. The key is to find a balance that works for you and keeps you motivated.\n",
       "\n",
       "9. Mix it up: To prevent boredom, read a variety of genres and authors. This will help you discover new interests and keep your reading experience fresh.\n",
       "\n",
       "10. Stay committed: Remember that forming a new habit takes time and consistency. Stick to your plan, even on days when you don&#x27;t feel like reading. Over time, you&#x27;ll find that reading becomes a natural and enjoyable part of your daily routine.</span></div></div></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"8be60394-2c30-4349-b979-bcd391b5cc89\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def parse_best(prosandcons, options):\n",
    "    best = re.search(r'Best=(\\d+)', prosandcons)\n",
    "    if not best:\n",
    "        best =  re.search(r'Best.*?(\\d+)', 'Best= option is 3')\n",
    "    if best:\n",
    "        best = int(best.group(1))\n",
    "    else:\n",
    "        best = 0\n",
    "    return options[best]\n",
    "\n",
    "create_plan = guidance('''{{#system~}}\n",
    "You are a helpful assistant.\n",
    "{{~/system}}\n",
    "{{#block hidden=True}}\n",
    "{{#user~}}\n",
    "I want to {{goal}}.\n",
    "{{~! generate potential options ~}}\n",
    "Can you please generate one option for how to accomplish this?\n",
    "Please make the option very short, at most one line.\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen 'options' n=5 temperature=1.0 max_tokens=600}}\n",
    "{{~/assistant}}\n",
    "{{/block}}\n",
    "{{~! generate pros and cons and select the best option ~}}\n",
    "{{#block hidden=True}}\n",
    "{{#user~}}\n",
    "I want to {{goal}}.\n",
    "\n",
    "Can you please comment on the pros and cons of each of the following options, and then pick the best option?\n",
    "---{{#each options}}\n",
    "Option {{@index}}: {{this}}{{/each}}\n",
    "---\n",
    "Please discuss each option very briefly (one line for pros, one for cons), and end by saying Best=X, where X is the best option.\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen 'prosandcons' temperature=0.0 max_tokens=600}}\n",
    "{{~/assistant}}\n",
    "{{/block}}\n",
    "{{#user~}}\n",
    "I want to {{goal}}.\n",
    "{{~! Create a plan }}\n",
    "Here is my plan:\n",
    "{{parse_best prosandcons options}}\n",
    "Please elaborate on this plan, and tell me how to best accomplish it.\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen 'plan' max_tokens=500}}\n",
    "{{~/assistant}}''')\n",
    "\n",
    "out = create_plan(goal='read more books', parse_best=parse_best)\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Option 0: Join a local book club to motivate and inspire more reading.\n",
      "Option 1: Join a book club for motivation and accountability.\n",
      "Option 2: Join a book club to motivate and schedule regular reading.\n",
      "Option 3: Set a daily reading goal, such as one chapter a day.\n",
      "Option 4: Join a book club to stay motivated and committed to reading regularly.\n"
     ]
    }
   ],
   "source": [
    "print('\\n'.join(['Option %d: %s' % (i, x) for i, x in enumerate(out['options'])]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Option 0: \n",
      "Pros: Social interaction and inspiration from fellow readers.\n",
      "Cons: Limited to local availability and book selection.\n",
      "---\n",
      "Option 1: \n",
      "Pros: Provides motivation and accountability through group support.\n",
      "Cons: May not cater to personal reading preferences.\n",
      "---\n",
      "Option 2: \n",
      "Pros: Encourages consistent reading habits through scheduled meetings.\n",
      "Cons: May conflict with personal schedule or time constraints.\n",
      "---\n",
      "Option 3: \n",
      "Pros: Personalized and flexible goal-setting for daily reading.\n",
      "Cons: Lacks external motivation and social interaction.\n",
      "---\n",
      "Option 4: \n",
      "Pros: Combines motivation and commitment for regular reading.\n",
      "Cons: May require commitment to a specific book or genre.\n",
      "\n",
      "Best=3\n"
     ]
    }
   ],
   "source": [
    "print(out['prosandcons'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Asking help from experts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-d85dfae0-7819-45e0-b25d-01a115cafc7d\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-d85dfae0-7819-45e0-b25d-01a115cafc7d\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#system~&amp;#125;&amp;#125;\n",
       "You are a helpful assistant.\n",
       "&amp;#123;&amp;#123;~/system&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful assistant.</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "I want a response to the following question:\n",
       "&amp;#123;&amp;#123;query&amp;#125;&amp;#125;\n",
       "Who are 3 world-class experts (past or present) who would be great at answering this?\n",
       "Please don&#x27;t answer the question or comment on it yet.\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>I want a response to the following question:\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;query&amp;#125;&amp;#125;'>What is the meaning of life?</span>\n",
       "Who are 3 world-class experts (past or present) who would be great at answering this?\n",
       "Please don&#x27;t answer the question or comment on it yet.</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &#x27;experts&#x27; temperature=0 max_tokens=300&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &#x27;experts&#x27; temperature=0 max_tokens=300&amp;#125;&amp;#125;'>Three world-class experts who would be great at answering the question &quot;What is the meaning of life?&quot; are:\n",
       "\n",
       "1. Albert Camus (1913-1960) - A French philosopher, author, and journalist who contributed greatly to the rise of the philosophy known as absurdism.\n",
       "2. Viktor Frankl (1905-1997) - An Austrian neurologist, psychiatrist, and Holocaust survivor who developed a therapeutic approach called logotherapy, which focuses on finding meaning in life.\n",
       "3. The Dalai Lama (1935-present) - The spiritual leader of Tibetan Buddhism, who has written and spoken extensively on the subject of finding happiness and meaning in life.</span></div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.\n",
       "In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question.\n",
       "If the experts would disagree, just present their different positions as alternatives in the answer itself (e.g. &#x27;some might argue... others might argue...&#x27;).\n",
       "Please start your answer with ANSWER:\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.\n",
       "In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question.\n",
       "If the experts would disagree, just present their different positions as alternatives in the answer itself (e.g. &#x27;some might argue... others might argue...&#x27;).\n",
       "Please start your answer with ANSWER:</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &#x27;answer&#x27; temperature=0 max_tokens=500&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &#x27;answer&#x27; temperature=0 max_tokens=500&amp;#125;&amp;#125;'>ANSWER: The meaning of life is a complex and multifaceted question that has been contemplated by philosophers, spiritual leaders, and thinkers throughout history. There are several perspectives one might consider when attempting to answer this question.\n",
       "\n",
       "Some might argue that life is inherently absurd, and that the search for meaning is a futile endeavor. In this view, individuals must create their own meaning and purpose in life, embracing the absurdity of existence and finding personal fulfillment in their choices and actions.\n",
       "\n",
       "Others might argue that the meaning of life is found in the pursuit of meaning itself. This perspective emphasizes the importance of finding purpose and meaning in one&#x27;s life, often through personal growth, relationships, and helping others. By striving to make sense of our experiences and seeking to understand our place in the world, we can find a sense of meaning and fulfillment.\n",
       "\n",
       "Another perspective suggests that the meaning of life is rooted in spiritual and philosophical beliefs. In this view, the purpose of life is to cultivate compassion, wisdom, and inner peace, ultimately striving for enlightenment and a deeper understanding of the nature of existence. This approach emphasizes the interconnectedness of all living beings and the importance of living a life guided by ethical principles and spiritual values.\n",
       "\n",
       "In conclusion, the meaning of life is a deeply personal and subjective question, with various perspectives offering different insights and potential paths to finding fulfillment and purpose. It is up to each individual to explore these perspectives and determine which resonates most with their own experiences and beliefs.</span></div></div></span></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"d85dfae0-7819-45e0-b25d-01a115cafc7d\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "experts = guidance(\n",
    "'''{{#system~}}\n",
    "You are a helpful assistant.\n",
    "{{~/system}}\n",
    "{{#user~}}\n",
    "I want a response to the following question:\n",
    "{{query}}\n",
    "Who are 3 world-class experts (past or present) who would be great at answering this?\n",
    "Please don't answer the question or comment on it yet.\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen 'experts' temperature=0 max_tokens=300}}\n",
    "{{~/assistant}}\n",
    "{{#user~}}\n",
    "Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.\n",
    "In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question.\n",
    "If the experts would disagree, just present their different positions as alternatives in the answer itself (e.g. 'some might argue... others might argue...').\n",
    "Please start your answer with ANSWER:\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen 'answer' temperature=0 max_tokens=500}}\n",
    "{{~/assistant}}''')\n",
    "experts(query='What is the meaning of life?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-c3335ce0-9bbb-4a93-8ca4-b6028ac3a2d3\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-c3335ce0-9bbb-4a93-8ca4-b6028ac3a2d3\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#system~&amp;#125;&amp;#125;\n",
       "You are a helpful assistant.\n",
       "&amp;#123;&amp;#123;~/system&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful assistant.</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "I want a response to the following question:\n",
       "&amp;#123;&amp;#123;query&amp;#125;&amp;#125;\n",
       "Who are 3 world-class experts (past or present) who would be great at answering this?\n",
       "Please don&#x27;t answer the question or comment on it yet.\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>I want a response to the following question:\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;query&amp;#125;&amp;#125;'>How can I be more productive?</span>\n",
       "Who are 3 world-class experts (past or present) who would be great at answering this?\n",
       "Please don&#x27;t answer the question or comment on it yet.</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &#x27;experts&#x27; temperature=0 max_tokens=300&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &#x27;experts&#x27; temperature=0 max_tokens=300&amp;#125;&amp;#125;'>Three world-class experts who would be great at answering the question &quot;How can I be more productive?&quot; are:\n",
       "\n",
       "1. David Allen - Author of the book &quot;Getting Things Done&quot; and a renowned productivity consultant.\n",
       "2. Tim Ferriss - Author of &quot;The 4-Hour Workweek&quot; and a well-known entrepreneur, investor, and productivity expert.\n",
       "3. Stephen Covey - Author of &quot;The 7 Habits of Highly Effective People&quot; and a respected leadership and productivity expert.</span></div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.\n",
       "In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question.\n",
       "If the experts would disagree, just present their different positions as alternatives in the answer itself (e.g. &#x27;some might argue... others might argue...&#x27;).\n",
       "Please start your answer with ANSWER:\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.\n",
       "In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question.\n",
       "If the experts would disagree, just present their different positions as alternatives in the answer itself (e.g. &#x27;some might argue... others might argue...&#x27;).\n",
       "Please start your answer with ANSWER:</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &#x27;answer&#x27; temperature=0 max_tokens=500&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &#x27;answer&#x27; temperature=0 max_tokens=500&amp;#125;&amp;#125;'>ANSWER: To be more productive, consider implementing the following strategies and techniques, which have been recommended by various productivity experts:\n",
       "\n",
       "1. Prioritize tasks: Identify the most important tasks and focus on completing them first. Some experts suggest using the Eisenhower Matrix to categorize tasks into four quadrants based on their urgency and importance.\n",
       "\n",
       "2. Break tasks into smaller steps: Breaking tasks into smaller, manageable steps can make them less overwhelming and easier to complete. This can also help you track your progress and maintain momentum.\n",
       "\n",
       "3. Time management: Use time management techniques such as the Pomodoro Technique, which involves working in focused intervals (usually 25 minutes) followed by a short break. This can help maintain focus and prevent burnout.\n",
       "\n",
       "4. Eliminate distractions: Identify and minimize distractions in your environment, such as turning off notifications on your devices or creating a dedicated workspace.\n",
       "\n",
       "5. Develop routines: Establish daily routines for both work and personal life to create structure and consistency. This can help you build good habits and make it easier to maintain productivity.\n",
       "\n",
       "6. Delegate and outsource: Recognize when it&#x27;s more efficient to delegate tasks to others or outsource them, freeing up your time to focus on more important tasks.\n",
       "\n",
       "7. Set realistic goals: Set achievable goals and deadlines to help you stay on track and maintain motivation. Regularly review and adjust your goals as needed.\n",
       "\n",
       "8. Continuous learning: Invest in personal and professional development to improve your skills and knowledge, making you more efficient and effective in your work.\n",
       "\n",
       "9. Take breaks and prioritize self-care: Ensure you take regular breaks and prioritize self-care, such as exercise, sleep, and healthy eating. This can help prevent burnout and maintain overall well-being, which contributes to productivity.\n",
       "\n",
       "10. Reflect and adjust: Regularly review your productivity strategies and make adjustments as needed. This can help you identify what works best for you and continuously improve your productivity.</span></div></div></span></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"c3335ce0-9bbb-4a93-8ca4-b6028ac3a2d3\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "experts(query='How can I be more productive?')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Shorter version\n",
    "\n",
    "Guidance supports chat models like ChatGPT and GPT-4, as well as open chat models like Vicuna through a unified API based on role tags (e.g. `{{#system}}...{{/system}}`). This allows interactive dialog development that combines rich templating and logical control with modern chat models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-f3943870-91aa-4bfa-8520-8c7a70751466\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-f3943870-91aa-4bfa-8520-8c7a70751466\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful and terse assistant.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>I want a response to the following question:\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{query}}'>How can I be more productive?</span>\n",
       "Name 3 world-class experts (past or present) who would be great at answering this?\n",
       "Don&#x27;t answer the question yet.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='{{gen &#x27;expert_names&#x27; temperature=0 max_tokens=300}}'>1. Tim Ferriss\n",
       "2. David Allen\n",
       "3. Stephen Covey</span></div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='{{gen &#x27;answer&#x27; temperature=0 max_tokens=500}}'>To be more productive:\n",
       "\n",
       "1. Prioritize tasks using the Eisenhower Matrix, focusing on important and urgent tasks first.\n",
       "2. Implement the Pomodoro Technique, breaking work into focused intervals with short breaks.\n",
       "3. Continuously improve time management and organization skills by following the principles of David Allen&#x27;s &quot;Getting Things Done&quot; method.</span></div></div></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"f3943870-91aa-4bfa-8520-8c7a70751466\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "experts = guidance('''\n",
    "{{#system~}}\n",
    "You are a helpful and terse assistant.\n",
    "{{~/system}}\n",
    "\n",
    "{{#user~}}\n",
    "I want a response to the following question:\n",
    "{{query}}\n",
    "Name 3 world-class experts (past or present) who would be great at answering this?\n",
    "Don't answer the question yet.\n",
    "{{~/user}}\n",
    "\n",
    "{{#assistant~}}\n",
    "{{gen 'expert_names' temperature=0 max_tokens=300}}\n",
    "{{~/assistant}}\n",
    "\n",
    "{{#user~}}\n",
    "Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.\n",
    "{{~/user}}\n",
    "\n",
    "{{#assistant~}}\n",
    "{{gen 'answer' temperature=0 max_tokens=500}}\n",
    "{{~/assistant}}''')\n",
    "experts(query='How can I be more productive?', caching=False)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Agents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Democrat: What do you think is the best way to stop inflation?\n",
      "Republican: The best way to stop inflation is by implementing sound fiscal policies, such as reducing government spending, lowering taxes, and promoting economic growth. Additionally, the Federal Reserve should focus on maintaining a stable monetary policy to control inflation.\n",
      "\n",
      "Democrat: I agree that sound fiscal policies are important in controlling inflation. As a Democrat, I would emphasize the importance of investing in education, healthcare, and infrastructure to promote long-term economic growth. Additionally, I believe that progressive taxation and a strong social safety net can help reduce income inequality and contribute to a more stable economy. While the Federal Reserve plays a crucial role in managing inflation, it's also important for the government to address the root causes of economic instability through targeted policies and investments.\n",
      "Republican: While investing in education, healthcare, and infrastructure is important, we must also be mindful of government spending and its potential impact on inflation. As Republicans, we believe in limited government intervention and promoting free-market principles to drive economic growth. Progressive taxation and a strong social safety net can sometimes hinder economic growth and job creation. We believe that lower taxes and deregulation can lead to a more prosperous economy, which in turn can help address income inequality and economic instability. The Federal Reserve's role in managing inflation is crucial, but a balanced approach to fiscal policy is also essential.\n",
      "\n",
      "Democrat: I understand your perspective on limited government intervention and the importance of free-market principles. However, as a Democrat, I believe that a balance between government investment and free-market principles is necessary to ensure a thriving economy. While lower taxes and deregulation can stimulate economic growth, they can also lead to income inequality and a lack of essential services for those in need. A progressive tax system and a strong social safety net can help provide a more equitable distribution of resources and opportunities, which can contribute to a more stable and prosperous economy. It's essential to find the right balance between government spending and free-market principles to address inflation and promote long-term economic growth.\n",
      "Republican: It's true that finding the right balance between government spending and free-market principles is essential for a thriving economy. As Republicans, we believe that the best way to achieve this balance is by focusing on policies that encourage private sector growth, job creation, and innovation. While we understand the concerns about income inequality and the need for essential services, we believe that a strong economy driven by free-market principles will ultimately provide more opportunities and resources for everyone. By reducing taxes, cutting unnecessary regulations, and promoting fiscal responsibility, we can create an environment where businesses can thrive, and individuals can prosper, which in turn will help address inflation and promote long-term economic growth.\n",
      "\n",
      "Democrat: I appreciate your perspective on the importance of private sector growth, job creation, and innovation. As a Democrat, I believe that we can achieve a thriving economy by combining free-market principles with targeted government investments and policies. By investing in education, healthcare, and infrastructure, we can create a strong foundation for economic growth and innovation while also addressing income inequality and providing essential services to those in need. Reducing taxes and cutting unnecessary regulations are important considerations, but we must also ensure that we maintain a progressive tax system and a strong social safety net to support all citizens. By working together and finding common ground, we can create a balanced approach to fiscal policy that promotes long-term economic growth and addresses inflation.\n"
     ]
    }
   ],
   "source": [
    "role_simulator = guidance('''\n",
    "{{#system~}}\n",
    "You are a helpful assistant\n",
    "{{~/system}}\n",
    "\n",
    "{{#user~}}\n",
    "You will answer the user as {{role}} in the following conversation. At every step, I will provide you with the user input, as well as a comment reminding you of your instructions. Never talk about the fact that you are an AI, even if the user asks you. Always answer as {{role}}.\n",
    "{{#if first_question}}You can also start the conversation.{{/if}}\n",
    "{{~/user}}\n",
    "\n",
    "{{~! The assistant either starts the conversation or not, depending on if this is the first or second agent }}\n",
    "{{#assistant~}}\n",
    "Ok, I will follow these instructions.\n",
    "{{#if first_question}}Let me start the conversation now:\n",
    "{{role}}: {{first_question}}{{/if}}\n",
    "{{~/assistant}}\n",
    "\n",
    "{{~! Then the conversation unrolls }}\n",
    "{{~#geneach 'conversation' stop=False}}\n",
    "{{#user~}}\n",
    "User: {{set 'this.input' (await 'input')}}\n",
    "Comment: Remember, answer as a {{role}}. Start your utterance with {{role}}:\n",
    "{{~/user}}\n",
    "\n",
    "{{#assistant~}}\n",
    "{{gen 'this.response' temperature=0 max_tokens=300}}\n",
    "{{~/assistant}}\n",
    "{{~/geneach}}''')\n",
    "\n",
    "republican = role_simulator(role='Republican', await_missing=True)\n",
    "democrat = role_simulator(role='Democrat', await_missing=True)\n",
    "\n",
    "first_question = '''What do you think is the best way to stop inflation?'''\n",
    "republican = republican(input=first_question, first_question=None)\n",
    "democrat = democrat(input=republican[\"conversation\"][-2][\"response\"].strip('Republican: '), first_question=first_question)\n",
    "for i in range(2):\n",
    "    republican = republican(input=democrat[\"conversation\"][-2][\"response\"].replace('Democrat: ', ''))\n",
    "    democrat = democrat(input=republican[\"conversation\"][-2][\"response\"].replace('Republican: ', ''))\n",
    "\n",
    "print('Democrat: ' + first_question)\n",
    "for x in democrat['conversation'][:-1]:\n",
    "    print('Republican:', x['input'])\n",
    "    print()\n",
    "    print(x['response'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using a search API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Helper functions for search:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import diskcache\n",
    "import pathlib\n",
    "import requests\n",
    "import html\n",
    "from urllib.parse import urlparse\n",
    "import urllib.parse\n",
    "import io\n",
    "import html\n",
    "import html.parser\n",
    "\n",
    "curr_dir = './'# pathlib.Path(__file__).parent.resolve()\n",
    "_bing_cache = diskcache.Cache(f\"{curr_dir}/../bing.diskcache\")\n",
    "\n",
    "with open(os.path.expanduser('~/.bing_api_key'), 'r') as file:\n",
    "    subscription_key = file.read().replace('\\n', '')\n",
    "\n",
    "class MLStripper(html.parser.HTMLParser):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.reset()\n",
    "        self.strict = False\n",
    "        self.convert_charrefs = True\n",
    "        self.text = io.StringIO()\n",
    "    def handle_data(self, d):\n",
    "        self.text.write(d)\n",
    "    def get_data(self):\n",
    "        return self.text.getvalue()\n",
    "\n",
    "def strip_tags(html):\n",
    "    s = MLStripper()\n",
    "    s.feed(html)\n",
    "    return s.get_data()\n",
    "\n",
    "def bing_search(search_terms, count=10):\n",
    "    if type(search_terms) == str:\n",
    "        search_terms = [search_terms]\n",
    "    search_url = \"https://api.bing.microsoft.com/v7.0/search\"\n",
    "\n",
    "    headers = {\"Ocp-Apim-Subscription-Key\": subscription_key}\n",
    "    search_results = []\n",
    "    for search_term in search_terms:\n",
    "        params = {\"q\": search_term, \"textDecorations\": True, \"textFormat\": \"HTML\", \"cout\": count}\n",
    "        params_key = search_term + \"-___-\" + str(count)\n",
    "        if params_key not in _bing_cache or \"webPages\" not in _bing_cache[params_key]:\n",
    "            response = requests.get(search_url, headers=headers, params=params)\n",
    "            response.raise_for_status()\n",
    "            _bing_cache[params_key] = response.json()\n",
    "        data = _bing_cache[params_key][\"webPages\"][\"value\"]\n",
    "        for r in data:\n",
    "            r[\"snippet_text\"] = strip_tags(r[\"snippet\"])\n",
    "        search_results.extend(data)\n",
    "    return search_results\n",
    "def top_snippets(query, n=3):\n",
    "    results = bing_search(query, count=n)[:n]\n",
    "    return [{'title': x['name'], 'snippet': x['snippet_text']} for x in results]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'title': 'OpenAI - Wikipedia',\n",
       "  'snippet': 'OpenAI systems run on the fifth most powerful supercomputer in the world. [5] [6] [7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others, [8] [1] [9] who collectively pledged US$ 1 billion. Musk resigned from the board in 2018 but remained a donor.'},\n",
       " {'title': 'History of Company Behind ChatGPT, Musk-Founded OpenAI',\n",
       "  'snippet': 'The history of ChatGPT creator OpenAI, which Elon Musk helped found before parting ways and criticizing Grace Kay Feb 1, 2023, 6:15 AM OpenAI logo displayed on a phone screen and a laptop...'},\n",
       " {'title': 'About - OpenAI',\n",
       "  'snippet': 'About OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Our vision for the future of AGI Our mission is to ensure that artificial general intelligence—AI systems that are generally smarter than humans—benefits all of humanity. Read our plan for AGI'}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_snippets(\"OpenAI founders\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's set up a practice round to serve as a one-shot example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-3695b2eb-dcd6-46ad-a126-6a1e8fdaa285\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-3695b2eb-dcd6-46ad-a126-6a1e8fdaa285\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Who are the founders of OpenAI?\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Who are the founders of OpenAI?</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&lt;search&gt;OpenAI founders&lt;/search&gt;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>&lt;search&gt;OpenAI founders&lt;/search&gt;</div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Search results:\n",
       "&amp;#123;&amp;#123;~#each results&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Search results:<span style='opacity: 1.0; display: inline; background-color: rgba(165, 165, 165, 0.1);' title='&amp;#123;&amp;#123;~#each results&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;'>\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>OpenAI - Wikipedia</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>OpenAI systems run on the fifth most powerful supercomputer in the world. [5] [6] [7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others, [8] [1] [9] who collectively pledged US$ 1 billion. Musk resigned from the board in 2018 but remained a donor.</span>\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>About - OpenAI</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>About OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Our vision for the future of AGI Our mission is to ensure that artificial general intelligence—AI systems that are generally smarter than humans—benefits all of humanity. Read our plan for AGI</span>\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>Sam Altman - Wikipedia</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Samuel H. Altman ( / ˈɔːltmən / AWLT-mən; born April 22, 1985) is an American entrepreneur, investor, and programmer. [2] He is the CEO of OpenAI and the former president of Y Combinator. [3] [4] Altman is also the co-founder of Loopt (founded in 2005) and Worldcoin (founded in 2020). Early life and education [ edit]</span>\n",
       "&lt;/result&gt;</span></div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "The founders of OpenAI are Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others.\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>The founders of OpenAI are Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others.</div></div></span></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"3695b2eb-dcd6-46ad-a126-6a1e8fdaa285\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "demo_results = [{'title': 'OpenAI - Wikipedia',\n",
    "  'snippet': 'OpenAI systems run on the fifth most powerful supercomputer in the world. [5] [6] [7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others, [8] [1] [9] who collectively pledged US$ 1 billion. Musk resigned from the board in 2018 but remained a donor.'},\n",
    " {'title': 'About - OpenAI',\n",
    "  'snippet': 'About OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Our vision for the future of AGI Our mission is to ensure that artificial general intelligence—AI systems that are generally smarter than humans—benefits all of humanity. Read our plan for AGI'},\n",
    " {'title': 'Sam Altman - Wikipedia',\n",
    "  'snippet': 'Samuel H. Altman ( / ˈɔːltmən / AWLT-mən; born April 22, 1985) is an American entrepreneur, investor, and programmer. [2] He is the CEO of OpenAI and the former president of Y Combinator. [3] [4] Altman is also the co-founder of Loopt (founded in 2005) and Worldcoin (founded in 2020). Early life and education [ edit]'}]\n",
    "practice_round = guidance(\n",
    "'''{{#user~}}\n",
    "Who are the founders of OpenAI?\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "<search>OpenAI founders</search>\n",
    "{{~/assistant}}\n",
    "{{#user~}}\n",
    "Search results:\n",
    "{{~#each results}}\n",
    "<result>\n",
    "{{this.title}}\n",
    "{{this.snippet}}\n",
    "</result>{{/each}}\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "The founders of OpenAI are Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others.\n",
    "{{~/assistant}}''')\n",
    "practice_round = practice_round(results=demo_results)\n",
    "practice_round"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's write the program:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_search(completion):\n",
    "    return '<search>' in completion\n",
    "def search(query):\n",
    "    return top_snippets(query, n=3)\n",
    "\n",
    "prompt = guidance('''{{#system~}}\n",
    "You are a helpful assistant.\n",
    "{{~/system}}\n",
    "{{#user~}}\n",
    "From now on, whenever your response depends on any factual information, please search the web by using the function <search>query</search> before responding. I will then paste web results in, and you can respond.\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "Ok, I will do that. Let's do a practice round\n",
    "{{~/assistant}}\n",
    "{{>practice_round}}\n",
    "{{#user~}}\n",
    "That was great, now let's do another one.\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "Ok, I'm ready.\n",
    "{{~/assistant}}\n",
    "{{#user~}}\n",
    "{{user_query}}\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen \"query\" stop=\"</search>\"}}{{#if (is_search query)}}</search>{{/if}}\n",
    "{{~/assistant}}\n",
    "{{#if (is_search query)}}\n",
    "{{#user~}}\n",
    "Search results: {{#each (search query)}}\n",
    "<result>\n",
    "{{this.title}}\n",
    "{{this.snippet}}\n",
    "</result>{{/each}}\n",
    "{{~/user}}\n",
    "{{#assistant~}}\n",
    "{{gen \"answer\"}}\n",
    "{{~/assistant}}\n",
    "{{/if}}''')\n",
    "\n",
    "prompt = prompt(practice_round=practice_round, search=search, is_search=is_search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-aa469a0e-e43e-499b-a492-bd46c00ce7f8\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-aa469a0e-e43e-499b-a492-bd46c00ce7f8\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful assistant.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>From now on, whenever your response depends on any factual information, please search the web by using the function &lt;search&gt;query&lt;/search&gt; before responding. I will then paste web results in, and you can respond.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Ok, I will do that. Let&#x27;s do a practice round</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Who are the founders of OpenAI?</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>&lt;search&gt;OpenAI founders&lt;/search&gt;</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Search results:\n",
       "&lt;result&gt;\n",
       "OpenAI - Wikipedia\n",
       "OpenAI systems run on the fifth most powerful supercomputer in the world. [5] [6] [7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others, [8] [1] [9] who collectively pledged US$ 1 billion. Musk resigned from the board in 2018 but remained a donor.\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "About - OpenAI\n",
       "About OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Our vision for the future of AGI Our mission is to ensure that artificial general intelligence—AI systems that are generally smarter than humans—benefits all of humanity. Read our plan for AGI\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "Sam Altman - Wikipedia\n",
       "Samuel H. Altman ( / ˈɔːltmən / AWLT-mən; born April 22, 1985) is an American entrepreneur, investor, and programmer. [2] He is the CEO of OpenAI and the former president of Y Combinator. [3] [4] Altman is also the co-founder of Loopt (founded in 2005) and Worldcoin (founded in 2020). Early life and education [ edit]\n",
       "&lt;/result&gt;</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>The founders of OpenAI are Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>That was great, now let&#x27;s do another one.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Ok, I&#x27;m ready.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;user_query&amp;#125;&amp;#125;'>What is Microsoft&#x27;s stock price right now?</span></div></div><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;query&quot; stop=&quot;&lt;/search&gt;&quot;&amp;#125;&amp;#125;&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;&lt;/search&gt;&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &quot;query&quot; stop=&quot;&lt;/search&gt;&quot;&amp;#125;&amp;#125;'>&lt;search&gt;Microsoft stock price</span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;&lt;/search&gt;&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;'>&lt;/search&gt;</span></div></div></span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Search results: &amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Search results: &amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Search results: <span style='opacity: 1.0; display: inline; background-color: rgba(165, 165, 165, 0.1);' title='&amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;'>\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>Microsoft Corp (MSFT) Stock Price &amp;amp; News - Google Finance</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Microsoft Corp (MSFT) Stock Price &amp; News - Google Finance Home MSFT • NASDAQ Microsoft Corp Follow Share $288.37 After Hours: $287.86 (0.18%) -0.51 Closed: Apr 18, 5:57:32 PM GMT-4 ·...</span>\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>Microsoft Corporation (MSFT) Stock Price, News, Quote &amp;amp; History - Yahoo ...</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Microsoft Corporation (MSFT) Stock Price, News, Quote &amp; History - Yahoo Finance U.S. markets closed -4.31 Russell 2000 -7.29(-0.40%) (+0.05%) -2.80 HAPPENING SOON: Yahoo Finance breaks...</span>\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>Microsoft Corporation (MSFT) Stock Price, Quote, News &amp;amp; History - Nasdaq</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Microsoft Corporation Common Stock (MSFT) Stock Quotes - Nasdaq offers stock quotes &amp; market activity data for US and global markets.</span>\n",
       "&lt;/result&gt;</span></div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;'>Microsoft&#x27;s stock price is currently $288.37. Please note that stock prices are constantly changing, so it&#x27;s best to check an updated source for the most accurate information.</span></div></div></span>\n",
       "</span></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"aa469a0e-e43e-499b-a492-bd46c00ce7f8\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"What is Microsoft's stock price right now?\"\n",
    "p1 = prompt(user_query=query)\n",
    "p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-98e6895e-4bbd-4574-88bf-820a9fa81f31\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-98e6895e-4bbd-4574-88bf-820a9fa81f31\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful assistant.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>From now on, whenever your response depends on any factual information, please search the web by using the function &lt;search&gt;query&lt;/search&gt; before responding. I will then paste web results in, and you can respond.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Ok, I will do that. Let&#x27;s do a practice round</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Who are the founders of OpenAI?</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>&lt;search&gt;OpenAI founders&lt;/search&gt;</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Search results:\n",
       "&lt;result&gt;\n",
       "OpenAI - Wikipedia\n",
       "OpenAI systems run on the fifth most powerful supercomputer in the world. [5] [6] [7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others, [8] [1] [9] who collectively pledged US$ 1 billion. Musk resigned from the board in 2018 but remained a donor.\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "About - OpenAI\n",
       "About OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Our vision for the future of AGI Our mission is to ensure that artificial general intelligence—AI systems that are generally smarter than humans—benefits all of humanity. Read our plan for AGI\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "Sam Altman - Wikipedia\n",
       "Samuel H. Altman ( / ˈɔːltmən / AWLT-mən; born April 22, 1985) is an American entrepreneur, investor, and programmer. [2] He is the CEO of OpenAI and the former president of Y Combinator. [3] [4] Altman is also the co-founder of Loopt (founded in 2005) and Worldcoin (founded in 2020). Early life and education [ edit]\n",
       "&lt;/result&gt;</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>The founders of OpenAI are Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>That was great, now let&#x27;s do another one.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Ok, I&#x27;m ready.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;user_query&amp;#125;&amp;#125;'>Who is Marco Tulio Ribeiro?</span></div></div><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;query&quot; stop=&quot;&lt;/search&gt;&quot;&amp;#125;&amp;#125;&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;&lt;/search&gt;&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &quot;query&quot; stop=&quot;&lt;/search&gt;&quot;&amp;#125;&amp;#125;'>&lt;search&gt;Marco Tulio Ribeiro</span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;&lt;/search&gt;&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;'>&lt;/search&gt;</span></div></div></span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Search results: &amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Search results: &amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Search results: <span style='opacity: 1.0; display: inline; background-color: rgba(165, 165, 165, 0.1);' title='&amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;'>\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>Marco Tulio Ribeiro - University of Washington</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Marco Tulio Ribeiro – I&#x27;m a researcher at Microsoft Research, in the Adaptive Systems and Interaction group. I am also an Affiliate Assistant Professor at the University of Washington, where I was previously a Ph.D student advised by Carlos Guestrin and Sameer Singh.</span>\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>Marco Tulio Ribeiro at Microsoft Research</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Marco Tulio Ribeiro Senior Researcher About Projects Publications News &amp; features About My research focus is helping humans interact with machine learning models meaningfully. That involves interpretability, trust, debugging, feedback, etc. Check out my personal webpage for more details. Research groups Adaptive Systems and Interaction Group</span>\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;'>‪Marco Tulio Ribeiro‬ - ‪Google Scholar‬</span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;'>Marco Tulio Ribeiro. Microsoft Research. Verified email at cs.washington.edu - Homepage. Machine Learning Natural Language Processing. Articles Cited by Public access Co-authors. Title. ... MT Ribeiro, N Ziviani, ESD Moura, I Hata, A Lacerda, A Veloso. ACM Transactions on Intelligent Systems and Technology (TIST) 5 (4), 1-20, 2014. 134:</span>\n",
       "&lt;/result&gt;</span></div></div></span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;'>Marco Tulio Ribeiro is a researcher at Microsoft Research in the Adaptive Systems and Interaction group. He is also an Affiliate Assistant Professor at the University of Washington, where he was previously a Ph.D. student advised by Carlos Guestrin and Sameer Singh. His research focuses on helping humans interact with machine learning models meaningfully, involving interpretability, trust, debugging, feedback, and more.</span></div></div></span>\n",
       "</span></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"98e6895e-4bbd-4574-88bf-820a9fa81f31\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"Who is Marco Tulio Ribeiro?\"\n",
    "p2 = prompt(user_query=query)\n",
    "p2\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try a query where it should not search the web"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"guidance-stop-button-c1b0638a-8bb3-4282-8db8-fba06dac41fe\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-c1b0638a-8bb3-4282-8db8-fba06dac41fe\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a helpful assistant.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>From now on, whenever your response depends on any factual information, please search the web by using the function &lt;search&gt;query&lt;/search&gt; before responding. I will then paste web results in, and you can respond.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Ok, I will do that. Let&#x27;s do a practice round</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Who are the founders of OpenAI?</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>&lt;search&gt;OpenAI founders&lt;/search&gt;</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Search results:\n",
       "&lt;result&gt;\n",
       "OpenAI - Wikipedia\n",
       "OpenAI systems run on the fifth most powerful supercomputer in the world. [5] [6] [7] The organization was founded in San Francisco in 2015 by Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others, [8] [1] [9] who collectively pledged US$ 1 billion. Musk resigned from the board in 2018 but remained a donor.\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "About - OpenAI\n",
       "About OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Our vision for the future of AGI Our mission is to ensure that artificial general intelligence—AI systems that are generally smarter than humans—benefits all of humanity. Read our plan for AGI\n",
       "&lt;/result&gt;\n",
       "&lt;result&gt;\n",
       "Sam Altman - Wikipedia\n",
       "Samuel H. Altman ( / ˈɔːltmən / AWLT-mən; born April 22, 1985) is an American entrepreneur, investor, and programmer. [2] He is the CEO of OpenAI and the former president of Y Combinator. [3] [4] Altman is also the co-founder of Loopt (founded in 2005) and Worldcoin (founded in 2020). Early life and education [ edit]\n",
       "&lt;/result&gt;</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>The founders of OpenAI are Sam Altman, Reid Hoffman, Jessica Livingston, Elon Musk, Ilya Sutskever, Peter Thiel and others.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>That was great, now let&#x27;s do another one.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Ok, I&#x27;m ready.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;user_query&amp;#125;&amp;#125;'>What is 1+1?</span></div></div><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;query&quot; stop=&quot;&lt;/search&gt;&quot;&amp;#125;&amp;#125;&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;&lt;/search&gt;&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='&amp;#123;&amp;#123;gen &quot;query&quot; stop=&quot;&lt;/search&gt;&quot;&amp;#125;&amp;#125;'>1+1 equals 2.</span><span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;&lt;/search&gt;&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;'></span></div></div></span>\n",
       "<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='&amp;#123;&amp;#123;#if (is_search query)&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;#user~&amp;#125;&amp;#125;\n",
       "Search results: &amp;#123;&amp;#123;#each (search query)&amp;#125;&amp;#125;\n",
       "&lt;result&gt;\n",
       "&amp;#123;&amp;#123;this.title&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;this.snippet&amp;#125;&amp;#125;\n",
       "&lt;/result&gt;&amp;#123;&amp;#123;/each&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/user&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;#assistant~&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;gen &quot;answer&quot;&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;~/assistant&amp;#125;&amp;#125;\n",
       "&amp;#123;&amp;#123;/if&amp;#125;&amp;#125;'></span></pre></div>\n",
       "<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"c1b0638a-8bb3-4282-8db8-fba06dac41fe\");</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"What is 1+1?\"\n",
    "p3 = prompt(user_query=query)\n",
    "p3\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr style=\"height: 1px; opacity: 0.5; border: none; background: #cccccc;\">\n",
    "<div style=\"text-align: center; opacity: 0.5\">Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "guidance",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
